/**
 * @file bsp_wdt.c
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */
#include "hal_adapter.h"
#include "bsp_comm.h"

static IWDG_HandleTypeDef hiwdg = {0};

void Error_Handler(void);

/**
 * @brief 独立看门狗初始化
 * 
 * @return 
 */
int8_t bsp_wdt_init(void)
{
    hiwdg.Instance = IWDG;
    hiwdg.Init.Prescaler = IWDG_PRESCALER_512;
    hiwdg.Init.Window = 4095;
    hiwdg.Init.Reload = 4095;
    hiwdg.Init.EWI = 0;
    if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
    {
        Error_Handler();
    }
    return 0;
}


/**
 * @brief 获取看门狗运行状态
 * 
 * @return int32_t 
 */
uint32_t bsp_wdt_status(void)
{
    uint32_t status = 0;

#ifdef IWDG_SR_ONF // TODO: u575 mcu need to reimplement this function
    /* Get back ONF flag */
    status = (hiwdg.Instance->SR & IWDG_SR_ONF);
#endif
    
    /* Return status */
    return status;    
}


/**
 * @brief 喂狗
 * 
 * @return int32_t 
 */
int32_t bsp_wdt_feed(void)
{
    /* Reload IWDG counter with value defined in the reload register */
    __HAL_IWDG_RELOAD_COUNTER(&hiwdg);

    /* Return function status */
    return 0;
}